import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:robot3/services.dart';
import 'package:robot3/widgets.dart';
import '../viewmodel/map_provider.dart';

class MapWidget extends StatelessWidget {
  const MapWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return Consumer<MapProvider>(
      // Listen to changes in AddQAProvider
      builder: (context, provider, child) {
        final maps = context.watch<MapProvider>().maps;
        final points = context.watch<MapProvider>().points;
        // final ok1 = context.watch<MapProvider>().ok1;
        return Column(
          spacing: 30 / ratio,
          children: [
            Row(
              children: [
                Text(
                  "        地图",
                  style: TextStyle(
                    color: MyBlack,
                    fontSize: 24 / ratio,
                    fontWeight: FontWeight.normal,
                  ),
                ),
                SizedBox(width: 20 / ratio),
                SizedBox(
                  width: 374 / ratio,
                  height: 44 / ratio,
                  child: GestureDetector(
                    onTap: () {
                      print("1");
                    },
                    child: RobotPopupMenuButton2(
                      init: provider.selectedMap,
                      map: maps,
                      width: 374 / ratio,
                      height: 44 / ratio,
                      itemHeight: 80 / ratio,
                      offsetY: 44 / ratio,
                      onItemSelected: (type) {
                        provider.setSelectedMap(type);
                        print(type);
                      },
                    ),
                  ),
                ),
              ],
            ),

            Row(
              children: [
                Text(
                  "        地点",
                  style: TextStyle(
                    color: MyBlack,
                    fontSize: 24 / ratio,
                    fontWeight: FontWeight.normal,
                  ),
                ),
                SizedBox(width: 20 / ratio),
                SizedBox(
                  width: 374 / ratio,
                  height: 44 / ratio,
                  child: GestureDetector(
                    onTap: () {
                      print("1");
                    },
                    child: RobotPopupMenuButton2(
                      init: provider.selectedPoint,
                      map: points,
                      width: 374 / ratio,
                      height: 44 / ratio,
                      itemHeight: 80 / ratio,
                      offsetY: 44 / ratio,
                      onItemSelected: (type) {
                        provider.setSelectedPoint(type);
                        print(type);
                      },
                    ),
                  ),
                ),
              ],
            ),

            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  "    关键词",
                  style: TextStyle(
                    color: MyBlack,
                    fontSize: 24 / ratio,
                    fontWeight: FontWeight.normal,
                  ),
                ),
                SizedBox(width: 20 / ratio),
                Column(
                  spacing: 20 / ratio,
                  children: List.generate(provider.keyowrds.length, (index) {
                    final controller = TextEditingController(
                      text: provider.keyowrds[index],
                    );

                    // Add a listener to the controller
                    controller.addListener(() {
                      // This is triggered every time the text changes
                      print("Text changed to: ${controller.text}");
                      provider.updateContent(index, controller.text);
                    });
                    return Row(
                      children: [
                        SizedBox(
                          width: 374 / ratio,
                          height: 44 / ratio,
                          child: RobotEditText(
                            controller: controller,
                            onTextChanged: (text) {},
                          ),
                        ),

                        // if(index!=0)
                        SizedBox(width: 42 / ratio),
                        // if(index!=0)
                        Ink(
                          width: 40 / ratio,
                          height: 40 / ratio,
                          decoration: const ShapeDecoration(
                            color: Colors.black12, // A constant color value
                            shape: CircleBorder(),
                          ),
                          child: SizedBox(
                            width: 40 / ratio,
                            height: 40 / ratio,
                            child: IconButton(
                              padding: EdgeInsets.zero,
                              iconSize: 40 / ratio,
                              icon: Icon(index == 0 ? Icons.add : Icons.remove),
                              color: Colors.black,
                              onPressed: () {
                                if (index == 0) {
                                  provider.addSize();
                                } else {
                                  provider.removeAt(index);
                                }
                              },
                            ),
                          ),
                        ),
                      ],
                    );
                  }),
                ),
              ],
            ),

            Row(
              children: [
                Text(
                  "问路播报",
                  style: TextStyle(
                    color: MyBlack,
                    fontSize: 24 / ratio,
                    fontWeight: FontWeight.normal,
                  ),
                ),
                SizedBox(width: 20 / ratio),
                SizedBox(
                  width: 374 / ratio,
                  height: 44 / ratio,
                  child: CharacterCountInput(
                    init: provider.ask,
                    onTextChanged: (text) {
                      provider.ask = text;
                    },
                  ),
                ),
              ],
            ),

            Row(
              children: [
                Text(
                  "出发播报",
                  style: TextStyle(
                    color: MyBlack,
                    fontSize: 24 / ratio,
                    fontWeight: FontWeight.normal,
                  ),
                ),
                SizedBox(width: 20 / ratio),
                SizedBox(
                  width: 374 / ratio,
                  height: 44 / ratio,
                  child: CharacterCountInput(
                    init: provider.start,
                    onTextChanged: (text) {
                      provider.start = text;
                    },
                  ),
                ),
              ],
            ),

            Row(
              children: [
                Text(
                  "到达播报",
                  style: TextStyle(
                    color: MyBlack,
                    fontSize: 24 / ratio,
                    fontWeight: FontWeight.normal,
                  ),
                ),
                SizedBox(width: 20 / ratio),
                SizedBox(
                  width: 374 / ratio,
                  height: 44 / ratio,
                  child: CharacterCountInput(
                    init: provider.arrive,
                    onTextChanged: (text) {
                      provider.arrive = text;
                    },
                  ),
                ),
              ],
            ),

            Row(
              children: [
                Text(
                  "  到达后原地等待",
                  style: TextStyle(
                    color: MyBlack,
                    fontSize: 24 / ratio,
                    fontWeight: FontWeight.normal,
                  ),
                ),
  RobotSwitch(selected: provider.ok1, onToggle: provider.setOk1),
              ],
            ),
          ],
        );
      },
    );
  }
}
